package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.api.travelstandard.service.IMbTravelstandCarModelService;
import com.corpgovernment.api.travelstandard.vo.car.MbTravelstandCarModelVo;
import com.corpgovernment.common.base.AbstractBaseService;
import com.corpgovernment.common.base.Page;
import com.corpgovernment.common.utils.ListUtils;
import com.corpgovernment.common.utils.ObjectUtils;
import com.corpgovernment.common.utils.PageUtils;
import com.corpgovernment.travelstandard.entity.db.MbTravelstandCarModel;
import com.corpgovernment.travelstandard.mapper.MbTravelstandCarModelMapper;
import com.ctrip.corp.obt.shard.context.PageContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MbTravelstandCarModelService extends AbstractBaseService implements IMbTravelstandCarModelService {

    @Autowired
    private MbTravelstandCarModelMapper mbTravelstandCarModelMapper;

    @Override
    public void insert(MbTravelstandCarModelVo model) {
        MbTravelstandCarModel insertParam = ObjectUtils.copyProperties(model, MbTravelstandCarModel.class);
        if (insertParam == null) {
            return;
        }
        mbTravelstandCarModelMapper.insertSelective(insertParam);
    }

    @Override
    public int delete(Long id) {
        return mbTravelstandCarModelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(MbTravelstandCarModelVo model) {
        MbTravelstandCarModel updateParam =  ObjectUtils.copyProperties(model, MbTravelstandCarModel.class);
        if (updateParam == null || updateParam.getId() == null) {
            return 0;
        }
        return mbTravelstandCarModelMapper.updateByPrimaryKeySelective(updateParam);
    }

    @Override
    public MbTravelstandCarModelVo get(Long id) {
        MbTravelstandCarModel result = mbTravelstandCarModelMapper.selectByPrimaryKey(id);
        if (result == null) {
            return null;
        }
        return  ObjectUtils.copyProperties(result, MbTravelstandCarModelVo.class);
    }

    @Override
    public List<MbTravelstandCarModelVo> list(MbTravelstandCarModelVo model) {
        return ListUtils.copyList(mbTravelstandCarModelMapper.list( ObjectUtils.copyProperties(model, MbTravelstandCarModel.class)), MbTravelstandCarModelVo.class);
    }

    @Override
    public int deleteByTsId(Long tsId) {
        return mbTravelstandCarModelMapper.deleteByTsId(tsId);
    }

    @Override
    public Page page(MbTravelstandCarModelVo model) {
        MbTravelstandCarModel param =  ObjectUtils.copyProperties(model, MbTravelstandCarModel.class);
        PageContext.startPage(model.getPageNum(), model.getPageSize());
        List<MbTravelstandCarModel> list = mbTravelstandCarModelMapper.list(param);
        return PageUtils.optPageBy(list, MbTravelstandCarModelVo.class);
    }
}
